package de.freudenreichdesign.webshop.persistence;

import java.util.List;

import javax.persistence.TransactionRequiredException;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import de.freudenreichdesign.webshop.persistence.hibernate.HibernateUtil;

public class DatabaseUtil {

	private static DatabaseUtil databaseInstance = null;
	private boolean isTransactionSaveStarted = false;
	private Session saveSession = null;
	
	private DatabaseUtil() {}
	
	public static DatabaseUtil getInstance() {
		if (databaseInstance == null) {
			databaseInstance = new DatabaseUtil();
		}
		return databaseInstance;
	}
	
	public Object loadObject(String className, long id) {
		//Session session = HibernateUtil.getSession();
		//session.beginTransaction();
		Object result = saveSession.get(className, id);
		//session.getTransaction().commit();
		//session.close();
		return result;
	}
	
	public Object getObject(String className, long id) {
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		Object result = session.get(className, id);
		session.getTransaction().commit();
		session.close();
		return result;
	}

	public boolean startSaveTransaction() {
		boolean status = false;
		if (isTransactionSaveStarted == false) {
			isTransactionSaveStarted = true;
			saveSession = HibernateUtil.getSession();
			saveSession.beginTransaction();
			status = true;
		}
		return status;
	}
	
	public void bufferBeforeSaveCommit(Object obj) throws HibernateException, TransactionRequiredException {
		if (isTransactionSaveStarted == true && saveSession != null) {
			//try {
				saveSession.saveOrUpdate(obj);
			/*} catch (Exception e) {
				saveSession.getTransaction().rollback();
				isTransactionSaveStarted = false;
				saveSession = null;
				throw new HibernateException("Saving was unsuccessful, a transaction rollback was executed!");
			}*/
		} else {
			throw new TransactionRequiredException("A transaction is required to save!");
		}
	}
	
	public long save(Object obj) throws HibernateException, TransactionRequiredException {
		if (isTransactionSaveStarted == true && saveSession != null) {
			//try {
				return (Long) saveSession.save(obj);
			/*} catch (Exception e) {
				saveSession.getTransaction().rollback();
				isTransactionSaveStarted = false;
				saveSession = null;
				throw new HibernateException("Saving was unsuccessful, a transaction rollback was executed!");
			}*/
		} else {
			throw new TransactionRequiredException("A transaction is required to save!");
		}
	}
	
	public void saveCommit() throws HibernateException, TransactionRequiredException {
		if (isTransactionSaveStarted == true && saveSession != null) {
			try {
				saveSession.getTransaction().commit();
				saveSession.close();
				isTransactionSaveStarted = false;
			} catch (Exception e) {
				saveSession.getTransaction().rollback();
				isTransactionSaveStarted = false;
				saveSession = null;
				throw new HibernateException("Committing was unsuccessful, a transaction rollback was executed!");
			}
		} else {
				throw new TransactionRequiredException("A transaction is required to save!");
		}
	}

	public void remove(Object obj) throws HibernateException {
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		try {
			session.delete(obj);
			session.getTransaction().commit();
			session.close();
		} catch (Exception e) {
			session.getTransaction().rollback();
			throw new HibernateException("Deleting was unsuccessful, a transaction rollback was executed!");
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Object> executeQuery(String query) {
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
		List<Object> result = session.createQuery(query).list();
		session.getTransaction().commit();
		session.close();
		return result;

	}
}
